package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

func init() {
	os.Setenv("FYNE_THEME", "light")
}

func main() {
	//新建一个app
	a := app.New()

	//设置程序图标
	a.SetIcon(resourceIcoPng)

	// 中文字体显示
	a.Settings().SetTheme(&myTheme{})

	//新建一个窗口
	w := a.NewWindow("批量创建目录工具 V0.1版")

	//主界面框架布局
	MainShow(w)
	//尺寸
	w.Resize(fyne.Size{Width: 600, Height: 400})
	//w居中显示
	w.CenterOnScreen()

	//窗口是否允许调整大小
	w.SetFixedSize(true)

	//循环运行
	w.ShowAndRun()
	err := os.Unsetenv("FYNE_FONT")
	if err != nil {
		return
	}
}

/**
判断文件是否存在
*/
func IsDir(fileAddr string) bool {
	s, err := os.Stat(fileAddr)
	if err != nil {
		//fmt.Println(err)
		return false
	}
	return s.IsDir()
}

func GetCurrentDirectory() string {
	//返回绝对路径  filepath.Dir(os.Args[0])去除最后一个元素的路径
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Println(err)
	}

	separator := string(os.PathSeparator)

	//将\替换成/
	return strings.Replace(dir, "\\", separator, -1)
}

// 顺序生成数字
func makeRange(min, max int) []int {
	a := make([]int, max-min+1)
	for i := range a {
		a[i] = min + i
	}
	return a
}

// MainShow 主界面函数
func MainShow(w fyne.Window) {

	separator := string(os.PathSeparator)

	Current_dir := GetCurrentDirectory()

	Qujian_entry := widget.NewEntry()
	Prefix_entry := widget.NewEntry()

	Folder_entry := widget.NewEntry()
	//设置文件夹路径为当前目录
	Folder_entry.SetText(Current_dir)

	Qujian_label := widget.NewLabel("请输入生成区间(例:1-100):")
	Prefix_label := widget.NewLabel("请输入前缀[例:101-2022-Y-DA]:")
	Folder_label := widget.NewLabel("生成目标文件夹:")

	kong_label := widget.NewLabel("*")

	Folder_open := widget.NewButton("选择目录", func() {
		dialog.ShowFolderOpen(func(list fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, w)
				return
			}
			if list == nil {
				log.Println("取消")
				return
			}
			//out := fmt.Sprintf(list.String())
			Folder_entry.SetText(list.Path())
		}, w)
	})

	bt3 := widget.NewButton("关闭", func() {
		w.Close()
	})

	bt4 := widget.NewButton("创建目录", func() {

		if len(Qujian_entry.Text) <= 0 {
			dialog.ShowInformation("提示", "请输入生成区间!", w)
			return
		}

		if len(Prefix_entry.Text) <= 0 {
			dialog.ShowInformation("提示", "请输入前缀!", w)
			return
		}

		if len(Folder_entry.Text) <= 0 {
			dialog.ShowInformation("提示", "请选择生成目标文件夹!", w)
			return
		}

		QujianSplit := strings.Split(Qujian_entry.Text, "-")

		_min, _ := strconv.Atoi(QujianSplit[0])
		_max, _ := strconv.Atoi(QujianSplit[1])
		a := makeRange(_min, _max)

		for _, dir := range a {

			dir := fmt.Sprintf("%03d", dir)

			_dir := Folder_entry.Text + separator + Prefix_entry.Text + "-" + dir

			err := os.MkdirAll(_dir, 0755)
			if err != nil {
				log.Printf("创建目录失败:[%v]\n", _dir)
			} else {
				log.Printf("创建目录成功:[%v]\n", _dir)
			}
		}

		dialog.ShowInformation("标题", "批量创建目录完成", w)
	})

	v1 := container.NewBorder(layout.NewSpacer(), layout.NewSpacer(), Qujian_label, kong_label, Qujian_entry)
	v2 := container.NewBorder(layout.NewSpacer(), layout.NewSpacer(), Prefix_label, kong_label, Prefix_entry)

	v3 := container.NewBorder(layout.NewSpacer(), layout.NewSpacer(), Folder_label, Folder_open, Folder_entry)

	v4 := container.NewHBox(bt3, bt4)
	v5Center := container.NewCenter(v4)

	ctnt := container.NewVBox(v1, v2, v3, v5Center)

	w.SetContent(ctnt)
}
